package com.mkj.container;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;

/**
 * @Author Arrebol
 * @Date 2024/8/6 18:49
 * @Project graphical programming
 * @Description：
 * 分割条组件演示
 */
public class SplitPaneTest {

    Book[] books = {
            new Book("Java自学宝典",new ImageIcon("swing\\image\\container\\java.png"),"国内关于Java编程最全面的图书，\b看得懂，学得会"),
            new Book("轻量级JavaEE企业应用实战",new ImageIcon("swing\\image\\container\\ee.png"),"SSM整合开发的经典图书，你值得拥有"),
            new Book("Android基础教程",new ImageIcon("swing\\image\\container\\android.png"),"全面介绍Android平台应用程序\n开发的各方面知识")
    };
    private final JFrame frame = new JFrame("测试分割条组件");
    //声明相关组件
    JList<Book> bookList = new JList<>(books);
    JLabel bookCover = new JLabel();
    JTextArea bookDescription = new JTextArea();
    public void init(){
        //组装视图
        //设定组件大小
        bookList.setPreferredSize(new Dimension(150,400));
        bookCover.setPreferredSize(new Dimension(220,270));
        bookDescription.setPreferredSize(new Dimension(220,130));
        //为JList设置条目选中监听器
        bookList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                //获取当前选中的是哪个条目
                Book book = bookList.getSelectedValue();
                //将书籍相关信息展示
                bookCover.setIcon(book.getIcon());
                bookDescription.setText(book.getDescription());
            }
        });
        //组装左边区域
        JSplitPane left = new JSplitPane(JSplitPane.VERTICAL_SPLIT,bookCover,new JScrollPane(bookDescription));
        //支持一触即展
        left.setOneTouchExpandable(true);
        JSplitPane whole = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,left,bookList);
        //支持连续布局
        whole.setContinuousLayout(true);
        whole.setDividerSize(10);
        frame.add(whole);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
    public static void main(String[] args) {
        new SplitPaneTest().init();
    }
}

class Book{
    private String name;
    private Icon icon;
    private String description;

    public Book() {}

    public Book(String name, Icon icon, String description) {
        this.name = name;
        this.icon = icon;
        this.description = description;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Icon getIcon() {
        return icon;
    }

    public void setIcon(Icon icon) {
        this.icon = icon;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Override
    public String toString() {
        return this.getName();
    }
}